package io.gitee.opabinia.generator;

import com.sun.codemodel.JCodeModel;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import org.jsonschema2pojo.DefaultGenerationConfig;
import org.jsonschema2pojo.GenerationConfig;
import org.jsonschema2pojo.Jackson2Annotator;
import org.jsonschema2pojo.SchemaGenerator;
import org.jsonschema2pojo.SchemaMapper;
import org.jsonschema2pojo.SchemaStore;
import org.jsonschema2pojo.rules.RuleFactory;

/**
 * Created by IntelliJ IDEA.
 *
 * @author eric 2022/8/2 16:14
 */
public class JsonSchema2Pojo {
    public static void main(String[] args) throws IOException {
        JCodeModel codeModel = new JCodeModel();
        String name = "/schema/required.json";
        String className = "ReqQueryRemainCount";
        String packageName = "com.example";
        File destDir = Path.of("D:\\Work\\CodeSrc").toFile();

        URL source = JsonSchema2Pojo.class.getResource(name);

        GenerationConfig config = new DefaultGenerationConfig() {
            @Override
            public boolean isGenerateBuilders() { // set config option by overriding method
                return true;
            }
        };

        SchemaMapper mapper = new SchemaMapper(
                new RuleFactory(config, new Jackson2Annotator(config),
                new SchemaStore()), new SchemaGenerator());
        mapper.generate(codeModel, className, packageName, source);

        codeModel.build(destDir);
    }
}
